﻿using FineAdmin.Common;
using FineAdmin.IService;
using FineAdmin.Model;
using HuiJI.Tools.TBK;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Web;
using System.Web.Mvc;

namespace FineAdmin.Web.Controllers
{
    public class BaseMobileController : Controller
    {
        public IOpRecordService OpRecordService { get; set; }
        public ISystemParameterService SystemParameterService { get; set; }

        public IAdZoneService AdZoneService { get; set; }

        public IPostsService PostsService { get; set; }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            

            //计算今日线报数量
            ViewBag.NewPostCount = PostsService.CountByWhere("where CreateTime >= '" + DateTime.Now.Date.ToString("yyyy-MM-dd HH:mm:ss") + "'");
            //

            bool isWX = false;
            string userAgent = Request.UserAgent;
            //如果为微信端登录，则需要网页授权，排除error控制器
            if (userAgent != null && userAgent.ToLower().IndexOf("micromessenger") > -1)
            {
                isWX = true;
            }
            else
            {

                isWX = false;
            }
            ViewBag.isWX = isWX;

            if (!Request.IsAjaxRequest())
            {
                OpRecordService.AddOpRecord("访问人次", "访问人次", 1);
                if (isWX)
                {
                    OpRecordService.AddOpRecord("微信", "访问来源", 1);
                }
                else
                {
                    OpRecordService.AddOpRecord("Mobile", "访问来源", 1);
                }
            }


            base.OnActionExecuting(filterContext);

        }

        public AdZoneModel DefaultAdZone
        {
            get
            {
                var adzList = AdZoneService.GetAdZoneList();
                var adzDefault = adzList.FirstOrDefault(m => m.IsDefault == true && m.Type == 1);
                return adzDefault;
            }
        }
        public TBKTools tbkTools
        {
            get
            {

                string Api_Url = SystemParameterService.GetByCode("TBK_API").SPValue;

                TBKTools tbk_tools = new TBKTools(Api_Url);
                return tbk_tools;

            }
        }

        protected const string SuccessText = "操作成功！";
        protected const string ErrorText = "操作失败！";
        /// <summary>
        /// 操作成功
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        protected virtual AjaxResult SuccessTip(string message = SuccessText)
        {
            return new AjaxResult { state = ResultType.success.ToString(), message = message };
        }
        /// <summary>
        /// 操作失败
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        protected virtual AjaxResult ErrorTip(string message = ErrorText)
        {
            return new AjaxResult { state = ResultType.error.ToString(), message = message };
        }

    }
}